package com.ctgu.tengyun.config;

import com.ctgu.tengyun.filter.H5XCrossAllowFilter;
import com.ctgu.tengyun.filter.SetCharacterEncodingFilter;
import com.ctgu.tengyun.filter.UserAuthorizationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;

/**
 * Created by liaoweizhi on 2019/7/17.
 */
@Configuration
public class WebFilterConfig {

    @Bean
    public FilterRegistrationBean setCharacterEncodingFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("setCharacterEncodingFilter");
        SetCharacterEncodingFilter greetingFilter = new SetCharacterEncodingFilter();
        registrationBean.setFilter(greetingFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(0);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean hiddenHttpMethodFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("hiddenHttpMethodFilter");
        HiddenHttpMethodFilter greetingFilter = new HiddenHttpMethodFilter();
        registrationBean.setFilter(greetingFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(10);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean h5XCrossAllowFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("h5XCrossAllowFilter");
        H5XCrossAllowFilter greetingFilter = new H5XCrossAllowFilter();
        registrationBean.setFilter(greetingFilter);
        registrationBean.addUrlPatterns("/api/*");
        registrationBean.setOrder(30);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean userAuthorizationFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("userAuthorizationFilter");
        UserAuthorizationFilter greetingFilter = new UserAuthorizationFilter();
        registrationBean.setFilter(greetingFilter);
        registrationBean.addUrlPatterns("/api/*");
        registrationBean.addInitParameter("exclusions", "/auth,/permission/role_resource,/test");//不需要校验token的路径
        registrationBean.setOrder(40);
        return registrationBean;
    }


}